package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserdaibiRBody struct {
	Data       []models.Userdaibi `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

type UserdaibiBody struct {
	Page     int   `json:"page"`
	Pagesize int   `json:"pagesize"`
	Uid      int64 `json:"uid" gorm:"uid"`
	Status   int64 `json:"status" gorm:"status"` // 1加 2减
	State    int64 `json:"state" gorm:"state"`   //1微信 2支付宝 3赏卷（帮别人点亮）4赏卷（自己全部点亮）5商品 6一番赏 7扭蛋机
}

func Userdaibi(ctx *gin.Context) {
	request := UserdaibiBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	state := request.State
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Userdaibi
	res := data_source.Db.Where("uid = ?", uid)
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Userdaibi{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserdaibiRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type UsercollectRBody struct {
	Data       []models.Collect `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

func Usercollect(ctx *gin.Context) {
	request := UserdaibiBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Collect
	res := data_source.Db.Where("uid = ?", uid)

	res.Model(&models.Collect{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UsercollectRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type UserordersRBody struct {
	Data       []models.Userorder `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

func Userorders(ctx *gin.Context) {
	request := UserdaibiBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Userorder
	res := data_source.Db.Where("uid = ?", uid)

	res.Model(&models.Userorder{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserordersRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type UsemojingRBody struct {
	Data       []models.Usermojing `json:"data"`
	PageNo     int                 `json:"pageNo"`
	PageSize   int                 `json:"pageSize"`
	TotalCount int64               `json:"totalCount"`
	TotalPage  int64               `json:"totalPage"`
}

func Usemojing(ctx *gin.Context) {
	request := UserdaibiBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	state := request.State
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Usermojing
	res := data_source.Db.Where("uid = ?", uid)
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Usermojing{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UsemojingRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type UserintegralRBody struct {
	Data       []models.Userintegral `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

func Userintegral(ctx *gin.Context) {
	request := UserdaibiBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Userintegral
	res := data_source.Db.Where("uid = ?", uid)
	res.Model(&models.Userintegral{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserintegralRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
